<?php

require_once("../core/lib/phpmailer/class.phpmailer.php");

class IndexController extends MainController implements ControllerInterface {

	public function ValidationAction($param, $as_action = true) {
		if ($as_action)
			$param['validation_data'] = array();

		$validator = new Validator($param['validation_data']);
		$validator->IsEmailAddress('emailAddr', 'Please enter email address');
		$action = Request::GetGet('id_f');

		if ($action == 'landingForm1') {
			$validator->IsEmpty('captcha1', 'Retype code');
			$validator->CheckCaptcha('captcha1', 'Retype code');
		} else {
			$validator->IsEmpty('captcha2', 'Retype code');
			$validator->CheckCaptcha2('captcha2', 'Retype code');
		}
		$this->smarty->assign('js_values', $validator->getJsValues());
		$this->smarty->assign('id_form', Request::GetGet('id_f'));
		return $validator->getErrorList();
	}

	public function indexAction($param) {
		$this->AddDescription("");
		$this->AddTitle("");

		if (Request::IsPost()) {
			$post = Request::GetAllPost(true);

			$e = Request::GetPost('emailAddr');
			if (!empty($e)) {
				$emails = $e;
			} else {
				$emails = array();

				$tmp = $post;
				if (isset($tmp['senderEmail']))
					unset($tmp['senderEmail']);

				foreach ($tmp as $item) {
					if ($this->isValidEmail($item)) {
						$emails[] = $item;
					}
				}
			}

			$this->sendMail($emails);
		}
		$this->AddScript('jquery-validate/jquery.validate.js');
		$this->AddScript(Router::GenerateUrl('Validation') . '?id_f=landingForm1');
		$this->AddScript(Router::GenerateUrl('Validation') . '?id_f=landingForm2');
	}

	public function PassionAction($param) {
		$this->AddDescription("");
		$this->AddTitle("");

		$this->smarty->assign('passion', 'selected');
	}

	public function PhilosophyAction($param) {
		$this->AddDescription("");
		$this->AddTitle("");

		$this->smarty->assign('philosophy', 'selected');
	}

	public function ServicesAction($param) {
		$this->AddDescription("");
		$this->AddTitle("");

		$this->smarty->assign('services', 'selected');
	}

	public function AfterApplyAction($param) {
		$this->AddDescription("");
		$this->AddTitle("");
	}

	public function AfterShareAction($param) {
		$this->AddDescription("");
		$this->AddTitle("");
	}

	function isValidEmail($email) {
		return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
	}

	private function sendMail($arrAddresses) {
		try {



			if (!is_array($arrAddresses)) { //zapisanie sie do newslettera
				$redirect = 'AfterApply';
				$mail = new PHPMailer();
				$mail->IsSMTP();

				$mail->SMTPAuth = true;
				$mail->IsHTML(1);
				$mail->FromName = "Tennizoom";
				$mail->From = "application@tennizoom.com";
				$mail->Host = "tennizoom.com";
				$mail->Username = "application@tennizoom.com";
				$mail->Password = "TenniZ00m_Notify2011";
				$mail->ClearAddresses();
				
				$date = date("Y-m-d H:i:s");
				$lpNs = new LpNewsletter();
				$lpNs->setEmail($arrAddresses);
				$lpNs->setRecommend(0);
				$lpNs->setHttpHost($_SERVER['HTTP_HOST']);
				$lpNs->setHttpRreferer($_SERVER['HTTP_REFERER']);
				$lpNs->setHttpUserAgent($_SERVER['HTTP_USER_AGENT']);
				$lpNs->setRemoteAddr($_SERVER['REMOTE_ADDR']);
				$lpNs->setDate($date);
				if (!empty($_SERVER['REMOTE_HOST']))
					$lpNs->setRemoteHost($_SERVER['REMOTE_HOST']);
				$id = LpNewsletterDAL::Save($lpNs->GetDalDataObj());

				$arrAddresses = array($arrAddresses);

				$mail->Subject = $this->smarty->fetch('partial/Index/mail/subjectApply.tpl');
				$mail->Body = $this->smarty->fetch('partial/Index/mail/bodyApply.tpl');
				foreach ($arrAddresses as $item) {
					$mail->AddAddress($item);
				}
				$mail->Send();
			} else { //powiadomienie znajomych o tennizoom
				$redirect = 'AfterShare';


				foreach ($arrAddresses as $item) {
					$date = date("Y-m-d H:i:s");
					$lpNs = new LpNewsletter();
					$lpNs->setEmail($item);
					$lpNs->setRecommend(1);
					$lpNs->setHttpHost($_SERVER['HTTP_HOST']);
					$lpNs->setHttpRreferer($_SERVER['HTTP_REFERER']);
					$lpNs->setHttpUserAgent($_SERVER['HTTP_USER_AGENT']);
					$lpNs->setRemoteAddr($_SERVER['REMOTE_ADDR']);
					$lpNs->setDate($date);
					$recommendedByName = Request::Get('senderName');
					$recommendedByEmail = Request::Get('senderEmail');
					$lpNs->setRecommendedByName($recommendedByName);
					$lpNs->setRecommendedByEmail($recommendedByEmail);
					if (!empty($_SERVER['REMOTE_HOST']))
						$lpNs->setRemoteHost($_SERVER['REMOTE_HOST']);

					$id = LpNewsletterDAL::Save($lpNs->GetDalDataObj());


					$content = Request::GetPost('senderMessage');
					$senderName = Request::GetPost('senderName');
					$senderEmail = Request::GetPost('senderEmail');

					$this->smarty->assign('senderName', $senderName);
					$this->smarty->assign('content', $content);
					$this->smarty->assign('senderEmail', $senderEmail);


					$mail = new PHPMailer();
					$mail->IsSMTP();

					$mail->Subject = $this->smarty->fetch('partial/Index/mail/subjectShare.tpl');
					$mail->Body = $this->smarty->fetch('partial/Index/mail/bodyShare.tpl');
					$mail->SMTPAuth = true;
					$mail->IsHTML(1);
					$mail->FromName = "Tennizoom";
					$mail->From = "notify@tennizoom.com";
					$mail->Host = "tennizoom.com";
					$mail->Username = "notify@tennizoom.com";
					$mail->Password = "TenniZ00m_Notify2011";
					$mail->ClearAddresses();
					$mail->AddAddress($item);
					$mail->Send();
				}
			}

			$this->AddRedirectInfo('', 'ok', Router::GenerateUrl($redirect));
		} catch (Exception $e) {
			
		}
	}

	public function PreDispatch($param) {
		$this->Run($param);
	}

	public function PostDispatch($param) {

		// nagłówek strony
		$this->RunShared('Header', $param);

		// footer
		$this->RunShared('Footer', $param);
	}

}

?>